package com.example.test1.controller;

import com.example.test1.bean.UserBean;
import com.example.test1.service.ExcelService;
import com.example.test1.service.UserService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class ExcelController {
    @Resource
    private ExcelService excelService;
    @Resource
    private UserService userService;

    @PostMapping(value = "/importExcel")
    public String importExcel(Model model,
                              @RequestParam("myfile") MultipartFile myFile){

        Integer nums = excelService.importExcel(myFile);

        PageInfo<UserBean> pageInfo = userService.selectUser4Page(2,3);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("userList",pageInfo.getList());
        return "users";
    }
    @RequestMapping(value = "/exportExcel",method = RequestMethod.GET)
    @ResponseBody
    public String showExportexcel(HttpServletRequest req,
                                  HttpServletResponse resp){
        excelService.exportExcelWithSimple(req,resp);
        return "users";
    }
    @RequestMapping(value = "/importExcel",method = RequestMethod.GET)
    public String showImportexcel() { return "importexcel"; }
}
